//
//  BVMyDetailOrderViewController.m
//  BuyVegetables
//
//  Created by 叶思超 on 15/10/13.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVMyDetailOrderViewController.h"
#import "BVConst.h"
#import "BVMyOrder.h"
#import "HttpTool.h"
#import "BVMyDetailOrder.h"
#import "BVMyDetailOrder_assort.h"
#import "MJExtension.h"
#import "NSString+Extension.h"
#import "UIView+Extension.h"
#import "BVMyOrderDetailCell.h"
#import "BVNextOrderViewController.h"
#import "BVAddAddressViewController.h"

@interface BVMyDetailOrderViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
 *  创建scrollView
 */
@property (nonatomic, strong) UIScrollView *scrollView;
/**
 *  底价券父视图
 */
@property (nonatomic, strong) UIView *discountView;
/**
 *  订单信息父视图
 */
@property (nonatomic, strong) UIView *detailOrderView;
/**
 *  接收详情情数据
 */
@property (nonatomic, strong) NSMutableArray *orderDetail;
/**
 *  添加tableview
 */
@property (nonatomic, strong) UITableView *tableView;
/**
 *  打电话
 */
@property (nonatomic, strong) UIWebView *phoneCallWebView;
/**
 *  蒙板
 */
@property (nonatomic, weak) UIButton *coverButton;

//@property (nonatomic,weak)BVAddAddressViewController *BVaddress;

@end

@implementation BVMyDetailOrderViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.pagingEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = BVColor(233, 234, 235);
        [self.scrollView addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)orderDetail {
    if (!_orderDetail) {
        _orderDetail = [NSMutableArray array];
    }
    return _orderDetail;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 64, BVScreenW, BVScreenH - 64);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航栏
    [self setupNav];
    
    // 获取订单详情数据
    [self loadShopDetailData];
}

#pragma mark -- 获取商品详情
- (void)loadShopDetailData {
    // 拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetablePayAPI/getOrderInfoById", Path];
    
    // 拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"order_id"] =  self.myOrder.order_id;
    
    [HttpTool get:url params:params success:^(id responseObj) {
        if ([responseObj[@"code"] isEqualToString:@"1"]) {
            BVLog(@"获取订单详情成功------%@", responseObj);
            
            BVMyDetailOrder *detailOrder = [BVMyDetailOrder objectWithKeyValues:responseObj[@"data"]];
            
//            NSLog(@"%@",responseObj[@"data"]);
            
            
            [self.orderDetail addObject:detailOrder];
            
            // 设置UI
            [self setupUI];
        } else {
            BVLog(@"获取订单详情失败－－－－－%@", responseObj);
        }
    } failure:^(NSError *error) {
        BVLog(@"请求失败%@", error);
    }];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BVMyDetailOrder *detailOrder = self.orderDetail[0];
    return detailOrder.order_assort.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 创建cell
    BVMyOrderDetailCell *cell = [BVMyOrderDetailCell cell];
    
    // 传递数据模型
    BVMyDetailOrder *detailOrder = self.orderDetail[0];
    cell.detailOrder = detailOrder.order_assort[indexPath.row];
    
    return cell;
}

#pragma mark -- 设置UI
- (void)setupUI {
    BVMyDetailOrder *detailOrder = self.orderDetail[0];
    
    // 1.商品详情
    self.tableView.frame = CGRectMake(0, 0, BVScreenW, 44 * detailOrder.order_assort.count);
    
    // 2.抵价券
    self.discountView = [[UIView alloc] init];
    self.discountView.frame = CGRectMake(0, CGRectGetMaxY(self.tableView.frame) + 15, BVScreenW, 40);
    self.discountView.backgroundColor = BVColor(233, 234, 235);
    [self.scrollView addSubview:self.discountView];
    
    // 2.1抵价券标识
    UILabel *discountLabel = [[UILabel alloc] init];
    discountLabel.frame = CGRectMake(10, 0, 60, 40);
    discountLabel.font = [UIFont systemFontOfSize:15];
    discountLabel.textColor = BVBaseOrangeColor;
    discountLabel.text = @"抵价券";
    [self.discountView addSubview:discountLabel];
    
    // 2.2抵价券钱数
    UILabel *discountPriceLabel = [[UILabel alloc] init];
    discountPriceLabel.frame = CGRectMake(CGRectGetMaxX(discountLabel.frame) + 10, 0, BVScreenW - CGRectGetMaxX(discountLabel.frame) - 10 - 10, 40);
    discountPriceLabel.font = [UIFont systemFontOfSize:15];
    discountPriceLabel.text = [NSString stringWithFormat:@"-¥%@", detailOrder.coupon_money];
    discountPriceLabel.textColor = BVBaseOrangeColor;
    discountPriceLabel.textAlignment = NSTextAlignmentRight;
    [self.discountView addSubview:discountPriceLabel];
    
    // 3.订单详情
    [self setupOrderDetail];
}

#pragma mark -- 设置订单详情
- (void)setupOrderDetail {
    
    BVMyDetailOrder *detailOrder = self.orderDetail[0];
    self.detailOrderView = [[UIView alloc] init];
    [self.scrollView addSubview:self.detailOrderView];
    self.detailOrderView.frame = CGRectMake(0, CGRectGetMaxY(self.discountView.frame) + 15, BVScreenW, 190);
    self.detailOrderView.backgroundColor = BVColor(233, 234, 235);
    
    // 订单编号
    UILabel *orderLabel = [[UILabel alloc] init];
    [self.detailOrderView addSubview:orderLabel];
    orderLabel.frame = CGRectMake(10, 0, BVScreenW - 20, 30);
    orderLabel.text = [NSString stringWithFormat:@"订单编号: %@", detailOrder.ordernum];
    orderLabel.font = [UIFont systemFontOfSize:15];
    
    // 订单时间
    UILabel *orderTimeLabel = [[UILabel alloc] init];
    [self.detailOrderView addSubview:orderTimeLabel];
    orderTimeLabel.frame = CGRectMake(10, CGRectGetMaxY(orderLabel.frame), BVScreenW - 20, 30);
    orderTimeLabel.text = [NSString stringWithFormat:@"订单时间: %@", detailOrder.order_time];
    orderTimeLabel.font = [UIFont systemFontOfSize:15];
    
    // 支付方式
    UILabel *payWayLabel = [[UILabel alloc] init];
    [self.detailOrderView addSubview:payWayLabel];
    payWayLabel.frame = CGRectMake(10, CGRectGetMaxY(orderTimeLabel.frame), BVScreenW - 20 , 30);
//    if ([detailOrder.pay_info isEqualToString:@""]) {
//        payWayLabel.text = @"支付方式: 在线支付";
//    } else {
//        payWayLabel.text = [NSString stringWithFormat:@"支付方式: %@", detailOrder.pay_info];
//    }
    payWayLabel.text = [NSString stringWithFormat:@"支付方式:%@", detailOrder.pay_info];
    payWayLabel.font = [UIFont systemFontOfSize:15];
    
    // 收货姓名
    UILabel *recevicedNameLabel = [[UILabel alloc] init];
    recevicedNameLabel.frame= CGRectMake(10, CGRectGetMaxY(payWayLabel.frame), BVScreenW - 20, 30);
    recevicedNameLabel.font = [UIFont systemFontOfSize:15];
    recevicedNameLabel.text = [NSString stringWithFormat:@"收货姓名: %@", detailOrder.receiver];;
    [self.detailOrderView addSubview:recevicedNameLabel];
    
    // 手机号码
    UILabel *phoneNumLable = [[UILabel alloc] init];
    phoneNumLable.frame = CGRectMake(10, CGRectGetMaxY(recevicedNameLabel.frame), BVScreenW - 20, 30);
    phoneNumLable.font = [UIFont systemFontOfSize:15];
    phoneNumLable.text = [NSString stringWithFormat:@"手机号码: %@", detailOrder.mobile];
    [self.detailOrderView addSubview:phoneNumLable];
    
    //收货地址标题
    UILabel *receviedAddressTitle = [[UILabel alloc]init];
    [self.detailOrderView addSubview:receviedAddressTitle];
    receviedAddressTitle.font = [UIFont systemFontOfSize:15];
    receviedAddressTitle.text = @"收货地址: ";
    receviedAddressTitle.sd_layout
    .topSpaceToView(phoneNumLable,0)
    .leftSpaceToView(self.detailOrderView,10)
    .widthIs(70)
    .heightIs(30);
    
    // 收获地址
    UILabel *receviedAddressLabel = [[UILabel alloc] init];
    [self.detailOrderView addSubview:receviedAddressLabel];
    NSString *addressStr = [NSString stringWithFormat:@"%@", detailOrder.address ];
    receviedAddressLabel.font = [UIFont systemFontOfSize:15];
    receviedAddressLabel.text = addressStr;
    receviedAddressLabel.sd_layout
    .topSpaceToView(phoneNumLable,0)
    .leftSpaceToView(receviedAddressTitle,0)
    .rightSpaceToView(self.detailOrderView,10)
    .autoHeightRatio(0);
    
    CGFloat detailOrderViewY =  CGRectGetMaxY(self.detailOrderView.frame);
    // 再来一单
    UIButton *nextOrderButton = [[UIButton alloc] init];
    nextOrderButton.layer.masksToBounds = YES;
    nextOrderButton.layer.cornerRadius = 3;
    nextOrderButton.layer.borderColor = [UIColor whiteColor].CGColor;
    nextOrderButton.layer.borderWidth = 1;
    nextOrderButton.backgroundColor = BVBaseOrangeColor;
    nextOrderButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [nextOrderButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextOrderButton setTitle:@"再来一单" forState:UIControlStateNormal];
    [nextOrderButton addTarget:self action:@selector(nextOrderButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    if (detailOrderViewY > BVScreenH - 80 - 64) {
        // 设置scrollviewframe值
        self.scrollView.contentSize = CGSizeMake(BVScreenW, CGRectGetMaxY(self.detailOrderView.frame) + 80);
        nextOrderButton.frame = CGRectMake(10, CGRectGetMaxY(self.detailOrderView.frame) + 20 , BVScreenW - 20, 40);
        [self.scrollView addSubview:nextOrderButton];
    } else {
        nextOrderButton.frame = CGRectMake(10, self.view.height - 50, BVScreenW - 20, 40);
        [self.view addSubview:nextOrderButton];
    }
}

#pragma mark -- 再来一单
- (void)nextOrderButtonClick {
    
    // if (订单详情里商品数据没有空的，执行这个方法)
    BVNextOrderViewController *nextOrderVC = [[BVNextOrderViewController alloc] init];
    nextOrderVC.myOrder = self.myOrder;
    ROOTNAVPUSH(nextOrderVC, YES);
}

#pragma mark -- 取消打电话
- (void)cancelButtonClick {
    [self.coverButton removeFromSuperview];
}

#pragma mark -- 打电话
- (void)phoneButtonClick:(UIButton *)button {
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",@"0531-58192080"]];
    if ( !_phoneCallWebView ) {
        _phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    }
    [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
}

#pragma mark -- 设置导航栏
- (void)setupNav {
    self.title = @"订单详情";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"wddd_tel"] style:UIBarButtonItemStyleDone target:self action:@selector(phoneButtonClick:)];
}

#pragma mark -- 返回
- (void)backClick {
    ROOTNAVPOP
}
@end
